//
//  TestBezierPathViewController.swift
//  TestAnimation
//
//  Created by xufei on 2019/4/1.
//  Copyright © 2019年 zjx. All rights reserved.
//

import UIKit
import CoreGraphics

class TestBezierPathViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = .white
        
        testDrawCurve()
    }
    
    // 如果仅仅是画曲线，可以简单的提供几个点
    
//    func drawCurveWith(points: [CGPoint]) {
//        var i = 0;
//        while i <
//    }
    
    func testDrawCurve() {
        
        var points: [CGPoint] = [[100, 100], [150, 150], [50, 200], [25, 250], [200, 300], [100, 350]]
        let bPath = UIBezierPath()
//        bPath.move(to: CGPoint(x: 100, y: 100));
        var loop = 0
        while ((loop + 3) < points.count) {
            let pt0 = points[loop]
            let pt1 = points[loop + 1]
            let pt2 = points[loop + 2]
            let pt3 = points[loop + 3]
            if loop == 0 {
                bPath.move(to: pt0)
            }

            JXCalculateBezierControllerPointer.getControlPointx0(pt0.x, andy0: pt0.y, x1: pt1.x, andy1: pt1.y, x2: pt2.x, andy2: pt2.y, x3: pt3.x, andy3: pt3.y, path: bPath)
            loop += 1;
        }
        
////        bPath.addCurve(to: CGPoint(x: 100, y: 400), controlPoint1: CGPoint(x: 300, y: 250), controlPoint2: CGPoint(x: 50, y: 360))
//
//        bPath.addQuadCurve(to: CGPoint(x: 100, y: 400), controlPoint: CGPoint(x: 300, y: 250))
////        bPath.lineWidth = 10
//
        
        
        let slayer = CAShapeLayer()
        slayer.strokeColor = UIColor.red.cgColor
        slayer.fillColor = UIColor.clear.cgColor
        slayer.lineWidth = 3
        slayer.path = bPath.cgPath
        slayer.frame = view.frame
        
        
        view.layer.addSublayer(slayer)
        
        let scalerLayer = JXCalculateBezierControllerPointer.layerOfScaler(withOriginPoint: [0, 0])
        
        let pointsPath = CGMutablePath()
        
        points.forEach {
            pointsPath.addRect(CGRect(x: $0.x - CGFloat(2), y: $0.y - CGFloat(2), width: 4, height: 4))
        }
        let pointLayer = CAShapeLayer()
        pointLayer.path = pointsPath
        pointLayer.backgroundColor = UIColor.clear.cgColor
        pointLayer.strokeColor = UIColor.blue.cgColor
        
        
        view.layer.addSublayer(pointLayer)
        view.layer.addSublayer(scalerLayer!)
    }

}


extension CGPoint: ExpressibleByArrayLiteral {
    public typealias ArrayLiteralElement = Int
    
    public init(arrayLiteral elements: CGPoint.ArrayLiteralElement...) {
        self.init(x: elements[0], y: elements[1])
    }
}








